using System;
using System.IO;
using Xtee.CodeDom.Filewriter;
using Xtee.Core;

namespace Xtee.CodeDom.MetaInfo
{ 
    public abstract class ClassInfo 
    { 
        protected ClassInfo(ClassNameInfo nameInfo)
        { 
            this.nameInfo = nameInfo;
        }
        protected bool Equals(ClassInfo other)
        {
            return Equals(nameInfo, other.nameInfo);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((ClassInfo) obj);
        }

        public virtual bool CanGenerate() {
            return true;
        }

        public SchemaObjectClassInfo NestContainer { get;   set; }
        public SchemaObjectClassInfo Parent { get;   set; }
        public ISchemaObjectResolver Resolver { get { return nameInfo.Resolver; } }
        public override int GetHashCode()
        {
            return nameInfo.GetHashCode();
        }

        public static bool operator ==(ClassInfo left, ClassInfo right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(ClassInfo left, ClassInfo right)
        {
            return !Equals(left, right);
        }

        public abstract ClassWriter CreateWriter(string rootPath, string serializersRootPath, CodeBuilderForm form);

        public virtual IFileNameResolver FileResolver
        {
            get
            {
                return new DefaultFileNameResolver();
            }
        }

        private readonly ClassNameInfo nameInfo;
        public ClassNameInfo GetNameInfo()
        {
            return nameInfo;
        }
    }
     
    public class DefaultFileNameResolver : IFileNameResolver
    {
        public FileInfo Resolve(ClassInfo classInfo, string rootPath, CodeBuilderForm form)
        {
            return classInfo.GetNameInfo().ToFile(rootPath, string.Empty, string.Empty, string.Empty);
        }
    }
      
}